<template>
	<view class="page">
		<checkbox-group @change="selected">
			<block v-for="(one,index) in list" :key="index">
				<view class="list-title">{{one.deptName}}（{{ one.members.length }}人）</view>
				<view class="item" v-for="member in one.members" :key="member.userId">
					<view class="key">{{member.name}}</view>
					<checkbox class="value" :value="member.userId" :checked="member.checked"></checkbox>
				</view>
			</block>
		</checkbox-group>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				members: []
			}
		},
		onLoad(option) {
			// 获取上一个页面url传参的参数
			if(option.hasOwnProperty('members')){
				let members = option.members
				this.members = members.split(',')
			}
		},
		onShow() {
			this.loadData(this)
		},
		methods: {
			loadData: function(ref) {
				ref.ajax(ref.url.searchUserGroupByDept, "POST", {
					keyword: ref.keyword
				}, function(resp) {
					let result = resp.data.result
					ref.list = result
					for (let dept of ref.list) {
						for (let member of dept.members) {
							if (ref.members.indexOf(member.userId + "") != -1) {
								member.checked = true
							} else {
								member.checked = false
							}
						}
					}
				})
			},
			selected(e) {
				let that = this
				// 获取选中的用户
				that.members = e.detail.value
				// 获取页面栈
				let pages = getCurrentPages()
				// 获取上一个页面
				let prevPage = pages[pages.length - 2]
				// 向页面传递数据
				prevPage.members = that.members
				prevPage.finishMembers = true
			}
		}
	}
</script>

<style lang="less">
	@import url("members.less");
</style>
